﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Announcement.Model;

namespace Announcement.Admin
{
    public partial class ApplyEdit : AdminLoginPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string mode = Request.QueryString["mode"];
                if (mode == "add")
                {
                    fwApply.ChangeMode(FormViewMode.Insert);
                }
                if (mode == "edit")
                {
                    fwApply.ChangeMode(FormViewMode.Edit);
                }
                if (mode == "read")
                {
                    //取得表单数据
                    ApplyTable at = new ApplyTable();
                    at.ContractCode = ((HiddenField)fwApply.FindControl("hfCCode")).Value;
                    at.Applicant = ((Label)fwApply.FindControl("ApplicantLabel")).Text;
                    at.HouseLocated = ((Label)fwApply.FindControl("HouseLocatedLabel")).Text;
                    at.HouseNum = ((Label)fwApply.FindControl("HouseNumLabel")).Text;
                    at.ApplyDate = DateTime.Parse(((Label)fwApply.FindControl("ApplyDateLabel")).Text);
                    at.GiftCode = ((Label)fwApply.FindControl("GiftCodeLabel")).Text;
                    at.Shifter = ((Label)fwApply.FindControl("ShifterLabel")).Text;
                    at.Gifter = ((Label)fwApply.FindControl("GifterLabel")).Text;

                    Session["ApplyTable"] = at;
                }
                
            }
        }

        protected void fwApply_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName == "Back")
            {
                Response.Redirect("~/Admin/ApplyMgr.aspx");
            }
            if (e.CommandName == "Cancel")
            {
                string idStr = Request.QueryString["id"];
                if (idStr == null)
                {
                    Response.Redirect("~/Admin/ApplyMgr.aspx");
                }
            }
            if (e.CommandName == "EditGarage")
            {
                //取得合同编号
                HiddenField hfCCode = (HiddenField)fwApply.FindControl("hfCCode");
                string cCode = hfCCode.Value;
                string id = Request.QueryString["Id"];
                string mode = Request.QueryString["mode"];
                Response.Redirect("~/Admin/GarageEdit.aspx?CCode=" + cCode + "&Id=" + id + "&mode=" + mode);
            }
            if (e.CommandName == "Announcement")
            {
                //跳转到相应打印界面
                RadioButtonList rblApplyType = (RadioButtonList)fwApply.FindControl("rblApplyType");
                int applyType = Convert.ToInt32(rblApplyType.SelectedValue);
                this.redirectPage(applyType);
            }
        }

        protected void fwApply_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            Response.Redirect("~/Admin/ApplyMgr.aspx");
        }

        protected void fwApply_DataBound(object sender, EventArgs e)
        {
            //若公告类型为车库销售公告，则把编辑车位按钮显示出来
            RadioButtonList rblApplyType = (RadioButtonList)fwApply.FindControl("rblApplyType");
            string selectValue = rblApplyType.SelectedValue;
            if (selectValue == ApplyVar.ApplyType.GARAGE.ToString())
            {
                LinkButton garageButton = (LinkButton)fwApply.FindControl("GarageButton");
                garageButton.Visible = true;
                //如果在只读模式下需要显示编辑车位则隐藏查看公告按钮
                if (Request.QueryString["mode"] == "read")
                {
                    LinkButton aButton = (LinkButton)fwApply.FindControl("AnnouncementButton");
                    aButton.Visible = false;
                }
            }
        }

        private void redirectPage(int applyType)
        {
            switch (applyType)
            {
                case ApplyVar.ApplyType.OWNER:
                    Response.Redirect("../Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.TOTAL:
                    Response.Redirect("../Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.HIS:
                    Response.Redirect("../Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.NOTICE:
                    Response.Redirect("../Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.MORTGAGE:
                    Response.Redirect("../Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.INITIAL:
                    Response.Redirect("../Lost.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.DEMOLITION:
                    Response.Redirect("../Logout.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.LEGAL:
                    Response.Redirect("../Logout.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.DIVORCE:
                    Response.Redirect("../Shift.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.GARAGE:
                    Response.Redirect("../Garage.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.PATCH:
                    Response.Redirect("../Repair.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.GIFT:
                    Response.Redirect("../GiftShift.aspx?isPrn=true");
                    break;
                case ApplyVar.ApplyType.OTHER:

                    break;
                default: break;
            }
        }

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            Session["AdminUserName"] = null;
            Response.Redirect("../Login.aspx");
        }

    }
}
